package com.ssyu.examination.conmmon.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyu.examination.conmmon.App;
import com.ssyu.examination.conmmon.AppServlet;
import com.ssyu.examination.conmmon.Constants;
import com.ssyu.examination.conmmon.entity.SysMenuEntity;
import com.ssyu.examination.conmmon.util.DBUtil;
import com.ssyu.examination.conmmon.util.FastjsonUtil;

@WebServlet("/SysMenuServlet")
public class SysMenuServlet extends AppServlet {
	private static final long serialVersionUID = 1L;

	public SysMenuServlet() {
		super();
	}

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding(Constants.ENCODE_UTF8);
		String command = request.getParameter("command");

		if ("listSysMenu".equals(command)) {
			listSysMenu(request, response);
		} else if ("getFatherMenu".equals(command)) {
			getFatherMenu(request, response);
		} else if ("addSysMenu".equals(command)) {
			addSysMenu(request, response);
		} else if ("getSysMenu".equals(command)) {
			getSysMenu(request, response);
		} else if ("updateSysMenu".equals(command)) {
			updateSysMenu(request, response);
		} else if ("getupdateState".equals(command)) {
			getupdateState(request, response);
		} else if ("checkTextSystem".equals(command)) {
			checkTextSystem(request, response);
		}
	}

	/**
	 * 菜单名称是否存在
	 * 
	 * @param request
	 * @param response
	 */
	private void checkTextSystem(HttpServletRequest request,
			HttpServletResponse response) {
		String text = request.getParameter("text");
		SysMenuEntity sysMenuEntity = DBUtil.getUniqueResult(
				SysMenuEntity.class, "select * from sys_menu where text=?",
				text);
		if (sysMenuEntity == null) {
			print(response, true);
		}
	}

	/**
	 * 设置失效
	 * 
	 * @param request
	 * @param response
	 */
	private void getupdateState(HttpServletRequest request,
			HttpServletResponse response) {
		String id = request.getParameter("id");
		int row = DBUtil.executeUpdate(
				"UPDATE sys_menu SET state=3 WHERE id=?", id);
		if (row == 1) {
			print(response, 1);
		}
	}

	/**
	 * 修改信息
	 * 
	 * @param request
	 * @param response
	 */
	private void updateSysMenu(HttpServletRequest request,
			HttpServletResponse response) {
		String id = request.getParameter("id");
		String text = request.getParameter("text");
		String url = request.getParameter("url");
		String sort = request.getParameter("sort");
		String state = request.getParameter("state");
		if ("open".equals(state)) {
			state = "1";
		} else if ("closed".equals(state)) {
			state = "2";
		} else {
			state = "3";
		}

		String memo = request.getParameter("memo");

		List<Object> list = new ArrayList<>();
		list.add(text);
		list.add(state);
		list.add(url);
		list.add(sort);
		list.add(memo);
		list.add(id);
		int row = DBUtil
				.executeUpdate(
						"UPDATE sys_menu SET text=?,state=?,url=?,sort=?,memo=? WHERE id=?",
						list);
		if (row == 1) {
			print(response, 1);
		}
	}

	/**
	 * 获取表单点击表单的信息
	 * 
	 * @param request
	 * @param response
	 */
	private void getSysMenu(HttpServletRequest request,
			HttpServletResponse response) {
		int id = 0;
		String Strid = request.getParameter("id");
		if (Strid != null) {
			id = Integer.parseInt(Strid);
		}
		SysMenuEntity sysMenuEntity = null;

		sysMenuEntity = DBUtil.getUniqueResult(SysMenuEntity.class,
				"SELECT * FROM sys_menu WHERE id = ?", id);
		int pid = sysMenuEntity.getPid();
		if (pid == 0) {
			sysMenuEntity = DBUtil
					.getUniqueResult(
							SysMenuEntity.class,
							"SELECT s1.*,s2.text as p__text FROM sys_menu AS s1,sys_menu AS s2 WHERE s1.id=s2.id AND s2.id=?",
							id);
		} else {
			sysMenuEntity = DBUtil
					.getUniqueResult(
							SysMenuEntity.class,
							"SELECT s1.*,s2.text as p__text FROM sys_menu AS s1,sys_menu AS s2 WHERE s1.pid=s2.id AND s1.id=?",
							id);
		}
		print(response, FastjsonUtil.toJson(sysMenuEntity));
	}

	/**
	 * 增加菜单
	 * 
	 * @param request
	 * @param response
	 */
	private void addSysMenu(HttpServletRequest request,
			HttpServletResponse response) {
		String pid = request.getParameter("id");
		String text = request.getParameter("text");
		String url = request.getParameter("url");
		String sort = request.getParameter("sort");
		String state = request.getParameter("state");
		String memo = request.getParameter("memo");
		int id = App.getOperateId(request, response);
		List<Object> list = new ArrayList<>();
		list.add(text);
		list.add(state);
		list.add(url);
		list.add(pid);
		list.add(sort);
		list.add(id);
		list.add(memo);

		int row = DBUtil
				.executeUpdate(
						"INSERT INTO sys_menu VALUES (null, ?, ?, 'icon-edit', ?, ?, ?, ?, now(), ?)",
						list);
		if (row == 1) {
			print(response, 1);
		}
	}

	/**
	 * 获取父类菜单
	 * 
	 * @param request
	 * @param response
	 */
	private void getFatherMenu(HttpServletRequest request,
			HttpServletResponse response) {
		int id = 0;
		String idStr = request.getParameter("id");
		if (idStr != null) {
			id = Integer.parseInt(idStr);
		}
		if (id == 0) {
			id = 1;
		}
		SysMenuEntity sysMenuEntity = null;
		sysMenuEntity = DBUtil
				.getUniqueResult(
						SysMenuEntity.class,
						"select sys_menu.id,sys_menu.text,sys_menu.pid from sys_menu where id=?",
						id);
		int pid = sysMenuEntity.getPid();
		if (pid == 0) {
			print(response, FastjsonUtil.toJson(sysMenuEntity));
		}
		sysMenuEntity = DBUtil.getUniqueResult(SysMenuEntity.class,
				"select * from sys_menu where id=?", pid);
		print(response, FastjsonUtil.toJson(sysMenuEntity));

	}

	/**
	 * 查询出系统菜单
	 * 
	 * @param request
	 * @param response
	 */
	private void listSysMenu(HttpServletRequest request,
			HttpServletResponse response) {
		int id = 0;
		String idStr = request.getParameter("id");
		if (idStr != null) {
			id = Integer.parseInt(idStr);
		}

		List<SysMenuEntity> sysMenuList = DBUtil
				.executeQuery(
						SysMenuEntity.class,
						"SELECT sys_menu.*,sys_user.name AS user__name FROM sys_menu,sys_user WHERE sys_user.id=sys_menu.opreate_user_id and sys_menu.state!=3 and pid=?",
						id);
		print(response, FastjsonUtil.toJsonWithDateFormat(sysMenuList,
				"yyyy-MM-dd HH:mm:ss"));
	}
}
